package fashionshop.gui.main;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.xnap.commons.gui.CloseableTabbedPane;

import fashionshop.gui.common.constant.GUIType;
import fashionshop.gui.common.context.UserContext;
import fashionshop.gui.common.event.LoginEvent;
import fashionshop.gui.common.event.LoginListener;
import fashionshop.gui.common.util.MessageBoxUtil;
import fashionshop.gui.main.manage.StoreManagePanel;
import fashionshop.gui.main.system.LoginDialog;
import fashionshop.gui.common.component.CommandToolBar;
import fashionshop.gui.common.component.DefaultCommandPanel;

@SuppressWarnings("serial")
public class FashionShopMainFrame extends JFrame {

	private JPanel contentPane;
	private JMenu menuSystem;
	private JMenuItem menuSystemLogin;
	private JMenuItem menuSystemLogout;
	private JMenuItem menuSystemExit;
	private JMenu menuManage;
	private JMenuItem menuManageUser;
	private JMenuItem menuManageStore;
	
	private LoginListener loginListener = new LoginListener() {
		
		@Override
		public void loginPerformed(LoginEvent evt) {
			MessageBoxUtil.info(FashionShopMainFrame.this, "Hello " + evt.getEmployee().getFullName() + "!");
			menuSystemLogin.setEnabled(false);
			changeGUIForEmployee(GUIType.ADMIN);
		}
	};
	private JPanel contentPanel;
	private CloseableTabbedPane mainTab;
	private JPanel commandPanel;
	private CommandToolBar commandToolBar;
	
	private void changeGUIForEmployee(GUIType type){
		switch (type) {
		case LOGOUT:
			menuSystemLogin.setEnabled(true);
			menuSystemLogout.setEnabled(false);
			break;
			
		case ADMIN:
			menuSystemLogin.setEnabled(false);
			menuSystemLogout.setEnabled(true);
			break;

		default:
			menuSystemLogin.setEnabled(false);
			menuSystemLogout.setEnabled(false);
			break;
		}
	}
	
	private void changeCommandToolBar(DefaultCommandPanel panel){
		commandToolBar.buildToolBar(panel.getCommandType(), panel.getCommandListener());
	}

	/**
	 * Create the frame.
	 */
	public FashionShopMainFrame() {
		initComponents();
	}
	
	private void initComponents() {
		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentShown(ComponentEvent e) {
				UserContext userContext = UserContext.getInstance();
				if(userContext.employeeLogin("admin", "admin")){
					MessageBoxUtil.info(FashionShopMainFrame.this, "Hello " + userContext.getEmployee().getFullName() + "!");
					changeGUIForEmployee(GUIType.ADMIN);
				}else{
					MessageBoxUtil.error(FashionShopMainFrame.this, "Tên đăng nhập hoặc mật khẩu không đúng");
				}
			}
		});
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 718, 429);
		setExtendedState(MAXIMIZED_BOTH);
		
				JMenuBar mainMenuBar = new JMenuBar();
		setJMenuBar(mainMenuBar);
		
		menuSystem = new JMenu("H\u1EC7 th\u1ED1ng");
		mainMenuBar.add(menuSystem);
		
		menuSystemLogin = new JMenuItem("\u0110\u0103ng nh\u1EADp");
		menuSystemLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				LoginDialog loginDialog = LoginDialog.getInstance(loginListener);
				loginDialog.setVisible(true);
			}
		});
		menuSystem.add(menuSystemLogin);
		
		menuSystemLogout = new JMenuItem("\u0110\u0103ng xu\u1EA5t");
		menuSystemLogout.setEnabled(false);
		menuSystem.add(menuSystemLogout);
		
		menuSystemExit = new JMenuItem("Tho\u00E1t");
		menuSystemExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		menuSystem.add(menuSystemExit);
		
		menuManage = new JMenu("Qu\u1EA3n l\u00FD");
		mainMenuBar.add(menuManage);
		
		menuManageUser = new JMenuItem("Qu\u1EA3n l\u00FD ng\u01B0\u1EDDi d\u00F9ng");
		menuManage.add(menuManageUser);
		
		menuManageStore = new JMenuItem("H\u1EC7 th\u1ED1ng c\u1EEDa h\u00E0ng");
		this.menuManageStore.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				mainTab.add(StoreManagePanel.getInstance(), "Quản lý cửa hàng");
				changeCommandToolBar(StoreManagePanel.getInstance());
			}
		});
		menuManage.add(menuManageStore);
		
				JMenuItem mntmFile = new JMenuItem("File");
		mntmFile.addActionListener(new ActionListener() {

			// @Override
			public void actionPerformed(ActionEvent e) {
			}
		});
		
				contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		changeGUIForEmployee(GUIType.LOGOUT);
		this.contentPane.setLayout(new BorderLayout(0, 0));
		
		commandPanel = new JPanel();
		this.contentPane.add(commandPanel, BorderLayout.NORTH);
		commandPanel.setLayout(new BorderLayout(0, 0));
		
		this.commandToolBar = CommandToolBar.getInstance();
		this.commandPanel.add(this.commandToolBar, BorderLayout.NORTH);
		
		this.contentPanel = new JPanel();
		this.contentPane.add(this.contentPanel, BorderLayout.CENTER);
		this.contentPanel.setLayout(new BorderLayout(0, 0));
		
		this.mainTab = new CloseableTabbedPane();
		this.contentPanel.add(this.mainTab);
	}

}
